package com.company;
class Node {
    public  int val;
    public Node next;

    public Node(int val) {
        this.val = val;
    }
}

public class Main {

    public Node head;

    public static Node reverseLinkedList(Node head) {
        if (head == null) {
            return null;
        }
        Node cur = head;
        Node pre = null;
        while (cur != null) {
            Node curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
        return pre;
    }

    public static void  print(Node head) {
        if (head == null) {
            return;
        }
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
	// write your code here
        Node head = new Node(1);
        Node node1 = new Node(2);
        Node node2 = new Node(3);
        head.next = node1;
        node1.next = node2;
        print(head);
        Node newHead = reverseLinkedList(head);
        print(head);
    }
}
